/**
 * @Author: 陈瑞鹏
 * @Date:   2022-10-17 16:09:38
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-10-31 18:21:15
 */

import abiao_utils from "abiao_utils";
import UserLoginLog from "../models/log/userLoginLogs.model";
import LoginLog from "../models/stats/loginLog.model";
import Retain from "../models/stats/retain.model";
import User from "../models/user/user.model";
import Gamedata from "../utils/stats/gamedata";

//游戏数据 记录
export default class log {

  constructor() {

  }

  /**
   * 登录日志记录
   */
  static async login_log_record(roleId: number, ip: string) {
      let date = abiao_utils.date(2)
      let GameUserRes: any = await User.findOne({ roleId });
      if (!GameUserRes) {
          return
      }

      let RetainRes: any = await Retain.findOne({ date });
      if (!RetainRes) {
          let info = {
              date,
          }
          await Retain.create(info);
      }
      RetainRes = await Retain.findOne({ date });

      // 新增用户
      let newUser = await LoginLog.findOne({ roleId });
      if (!newUser) {
          await Retain.updateOne({ date }, { DNU: Number(RetainRes.DNU) + 1 });
      }

      // 登录用户记录
      let LoginLogRes: any = await LoginLog.findOne({ roleId, loginDate: date });
      if (!LoginLogRes) {
          let info = {
              roleId,
              nickName: GameUserRes.nickName,
              serverId: GameUserRes.serverId,
              ip,
              loginDate: date,
          }
          await LoginLog.create(info);
          let RetainRes: any = await Retain.findOne({ date });
          await Retain.updateOne({ date }, { DAU: Number(RetainRes.DAU) + 1 });
          let createDate = GameUserRes.createTime.split(' ')[0];
          await Gamedata.retain_user_record(createDate, date);
      } else {
          await LoginLog.updateOne({ roleId, loginDate: date }, { loginNum: Number(LoginLogRes.loginNum) + 1 });
      }


      let loginInfo = {
          roleId,
          nickName: GameUserRes.nickName,
          serverId: GameUserRes.serverId,
          ip,
          loginDateTime: abiao_utils.date()
      }
      await UserLoginLog.create(loginInfo);
  }
}